package com.atguigu.ucenterservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.commonutils.utils.JwtUtils;
import com.atguigu.commonutils.vo.UcenterMemberPay;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.LoginVo;
import com.atguigu.ucenterservice.entity.vo.RegisterVo;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-30
 */
@Api(description="用户客户管理")
@RestController
@RequestMapping("/ucenterservice/member")
@CrossOrigin  //配置中心
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @ApiOperation(value = "用户注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "用户登入")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token =  memberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("getInfoByToken")
    public R getInfoByToken(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = memberService.getById(memberId);
        return R.ok().data("ucenterMember", ucenterMember);
    }

    //评论也可以
    @ApiOperation(value = "根据用户id获取用户信息，远程调用")
    @GetMapping("getUcenterInfoOrder/{memberId}")
    public UcenterMemberPay getUcenterInfoOrder(
            @PathVariable("memberId") String memberId
    ) {
        UcenterMember ucenterMember = memberService.getById(memberId);
        UcenterMemberPay ucenterMemberPay = new UcenterMemberPay();
        BeanUtils.copyProperties(ucenterMember, ucenterMemberPay);
        return ucenterMemberPay;
    }


    @ApiOperation(value = "根据日期统计用户注册信息,远程调用")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day) {
        Integer count = memberService.countRegister(day);
        return R.ok().data("countRegister", count);

    }


}

